//
//  UIViewController+STTools_NavigationTools.m
//  SportHome
//
//  Created by stoneobs on 17/1/11.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "UIViewController+STTools_NavigationTools.h"

@implementation UIViewController (STTools_NavigationTools)
- (void)stoneobs_fun_st_setLeftItemWithTitle:(NSString *)title
{
    UIBarButtonItem * stoneobs_temp_left = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStyleDone target:self action:@selector(stoneobs_fun_st_leftBarAction:)];
    self.navigationItem.leftBarButtonItem = stoneobs_temp_left;
}
- (void)stoneobs_fun_st_setLeftItemWithTitle:(NSString *)title titleColor:(UIColor*)color;
{

    UIBarButtonItem * stoneobs_temp_left =[[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(stoneobs_fun_st_leftBarAction:)];
    [stoneobs_temp_left setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    stoneobs_temp_left.tintColor = color;
    self.navigationItem.leftBarButtonItem = stoneobs_temp_left;
   
}
- (void)stoneobs_fun_st_setLeftItemWithImage:(UIImage *)image
{
    UIImage * stoneobs_temp_editimgae = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * stoneobs_temp_leftImageItem = [[UIBarButtonItem alloc]initWithImage:stoneobs_temp_editimgae style:UIBarButtonItemStyleDone target:self action:@selector(stoneobs_fun_st_leftBarAction:)];
    self.navigationItem.leftBarButtonItem = stoneobs_temp_leftImageItem;
}
- (void)stoneobs_fun_st_setLeftItemWithImage:(UIImage *)image andwithTitle:(NSString *)title  titleColor:(UIColor*)titleColor
{
    UIButton * stoneobs_temp_but =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
    [stoneobs_temp_but setImage:image forState:UIControlStateNormal];
    [stoneobs_temp_but setImage:image forState:UIControlStateSelected];
    [stoneobs_temp_but setImage:image forState:UIControlStateHighlighted];
    [stoneobs_temp_but setTitle:title forState:UIControlStateNormal];
    [stoneobs_temp_but setTitleColor:titleColor forState:UIControlStateNormal];
    stoneobs_temp_but.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    stoneobs_temp_but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [stoneobs_temp_but addTarget:self action:@selector(stoneobs_fun_st_leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * stoneobs_temp_left = [[UIBarButtonItem alloc]initWithCustomView:stoneobs_temp_but];
    self.navigationItem.leftBarButtonItem = stoneobs_temp_left;
}
- (void)stoneobs_fun_st_setRightItemWithTitle:(NSString *)title
{
    UIBarButtonItem * stoneobs_temp_right = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(stoneobs_fun_st_rightBarAction:)];
    stoneobs_temp_right.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = stoneobs_temp_right;
}
- (void)stoneobs_fun_st_setRightItemWithTitle:(NSString *)title titleColor:(UIColor*)color
{
    UIBarButtonItem * stoneobs_temp_right =[[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(stoneobs_fun_st_rightBarAction:)];
    [stoneobs_temp_right setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    [stoneobs_temp_right setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateSelected];
    stoneobs_temp_right.tintColor = color;
    self.navigationItem.rightBarButtonItem = stoneobs_temp_right;
}
- (void)stoneobs_fun_st_setRightItemWithImage:(UIImage *)image
{
    UIImage * stoneobs_temp_editimgae = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * stoneobs_temp_rightImage = [[UIBarButtonItem alloc]initWithImage:stoneobs_temp_editimgae style:UIBarButtonItemStyleDone target:self action:@selector(stoneobs_fun_st_rightBarAction:)];
    self.navigationItem.rightBarButtonItem = stoneobs_temp_rightImage;
}
- (void)stoneobs_fun_st_setRightItemWithImage:(UIImage *)image andwithTitle:(NSString *)title
{
    UIButton * stoneobs_temp_but = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
    [stoneobs_temp_but setImage:image forState:UIControlStateNormal];
    [stoneobs_temp_but setTitle:title forState:UIControlStateNormal];
    [stoneobs_temp_but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //but.contentEdgeInsets=UIEdgeInsetsMake(0, -20, 0, 25);
    [stoneobs_temp_but addTarget:self action:@selector(stoneobs_fun_st_rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * stoneobs_temp_right = [[UIBarButtonItem alloc]initWithCustomView:stoneobs_temp_but];
    self.navigationItem.rightBarButtonItem = stoneobs_temp_right;
}
- (void)stoneobs_fun_st_setLeftItemWithView:(UIView *)view
{
    UIBarButtonItem * stoneobs_temp_left =[[UIBarButtonItem alloc]initWithCustomView:view];
    UITapGestureRecognizer * stoneobs_temp_tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(stoneobs_fun_st_leftBarAction:)];
    [view addGestureRecognizer:stoneobs_temp_tap];
    self.navigationItem.leftBarButtonItem=stoneobs_temp_left;
}

- (void)stoneobs_fun_st_setRightItemWithView:(UIView *)view
{
    UIBarButtonItem * stoneobs_temp_right = [[UIBarButtonItem alloc]initWithCustomView:view];
    UITapGestureRecognizer * stoneobs_temp_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(stoneobs_fun_st_rightBarAction:)];
    [view addGestureRecognizer:stoneobs_temp_tap];
    self.navigationItem.rightBarButtonItem = stoneobs_temp_right;
}

- (void)stoneobs_fun_st_rightBarAction:(id)sender
{
    
}
- (void)stoneobs_fun_st_leftBarAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (UINavigationController*)st_currentNavgationController{
    UIViewController* rootvc = (id)UIApplication.sharedApplication.delegate.window.rootViewController;
//    if (rootvc.presentedViewController) {
//        rootvc = rootvc.presentedViewController;
//    }
    if ([rootvc isKindOfClass:UITabBarController.class]) {
        UITabBarController * tabbar =  (id)rootvc;
        UINavigationController * nav = (id)tabbar.selectedViewController;
        if ([nav isKindOfClass:UINavigationController.class]) {
            return nav;
        }
    }
    if ([rootvc isKindOfClass:UINavigationController.class]) {
        UINavigationController * nav = (id)rootvc;
        if ([nav isKindOfClass:UINavigationController.class]) {
            return nav;
        }
    }
    return nil;
}

//获取当前屏幕显示的viewcontroller
- (UIViewController *)st_getCurrentVC{
    
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)  {
        
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)  {
            
            if (tmpWin.windowLevel == UIWindowLevelNormal)  {
                
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] lastObject];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

@end
